package code.tools.commponents
{
	import code.untils.FiltersEnum;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	/**
	 *横向选项卡列表
	 * @author liangqian
	 * 
	 */	
	public class HList extends Component
	{
		public static const STYLE_ARROW_TOP:int = 10;
		public static const STYLE_ARROW_DOWN:int = 20;
		
		private var _num:int;
		private var _texts:Array;
		private var _clickFunction:Function;
		
		private var _hbox:HBox;
		private var _array:Array;
		private var _arrows:Array;
		/**
		 * 
		 * @param parent
		 * @param xpos
		 * @param ypos
		 * @param num 总共有多少个选项卡
		 * @param texts 每人选项卡上显示的名字
		 * @param arrows 箭头朝向数组，见本类枚举，若某个选项卡不显示箭头，则传-1
		 * @param clickFunction
		 * 
		 */		
		public function HList(parent:DisplayObjectContainer, xpos:Number, ypos:Number,num:int,texts:Array,arrows:Array,clickFunction:Function)
		{
			_num = num;
			_texts = texts;
			_arrows = arrows;
			_clickFunction = clickFunction;
			super(parent, xpos, ypos);
		}
		
		override protected function init():void
		{
			setSize(284,27);
			super.init();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			
			_hbox = new HBox(this);
			_hbox.spacing = 0;
			_array = [];
			var i:int;
			var centerNum:int = _num - 2;//减掉前后两个
			var item:HListItem = new HListItem(_hbox,HListItem.STYLE_LEFT,_arrows[i],_texts[i],clickHandler, i);//左
			_array.push(item);
			for(; i < centerNum; i++)
			{
				item = new HListItem(_hbox,HListItem.STYLE_CENTER,_arrows[i+1],_texts[i + 1],clickHandler,i + 1);//中
				_array.push(item);
			}
			i++;
			item = new HListItem(_hbox,HListItem.STYLE_RIGHT,_arrows[i],_texts[i],clickHandler, i);//右
			_array.push(item);
			
			_hbox.draw();
			_width = _hbox.width;
			
			filters = [FiltersEnum.CHARTS_RECIPE_ICON_DROPSHADOWFILTER];
		}
		
		private function clickHandler(index:int):void
		{
			clearSelectedStatus();
			(_array[index] as HListItem).selected = true;
			_clickFunction(index);
		}
		/**
		 *清除掉选中状态 
		 * 
		 */		
		public function clearSelectedStatus():void
		{
			var i:int;
			for(; i < _num; i++)
				(_array[i] as HListItem).selected = false;
		}
	}
}
import code.structure.manager.loader.DllLoaderManager;
import code.structure.manager.font.FontManager;
import code.tools.commponents.Component;
import code.tools.commponents.HBox;
import code.tools.commponents.HList;
import code.tools.commponents.Helpers;
import code.untils.FiltersEnum;
import code.untils.UiNameConst;

import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormatAlign;

class HListItem extends Component
{
	public static const STYLE_LEFT:int = 10;
	public static const STYLE_CENTER:int = 20;
	public static const STYLE_RIGHT:int = 30;
	
	private var _mc:MovieClip;
	private var _hb:HBox;
	private var _arrowMc:MovieClip;
	private var _nameTf:TextField;
	private var _text:String;
	private var _clickCallHandler:Function;
	
	private var _index:int ;
	function HListItem(parent:DisplayObjectContainer,style:int,arrowStyle:int,text:String,clickCallHandler:Function,index:int)
	{
		_text = text;
		_clickCallHandler = clickCallHandler;
		_index = index;
		
		buttonMode = true;
		mouseChildren = false;
		
		_hb = new HBox(this);
		_hb.spacing = 0;
		super(parent);
		
		var subKey:String;
		switch(style)
		{
			case STYLE_LEFT:
				subKey = 'hBoxList_left';
				break;
			case STYLE_CENTER:
				subKey = 'hBoxList_center';
				break;
			case STYLE_RIGHT:
				subKey = 'hBoxList_right';
				break;
		}
		
		var arrowSubKey:String;
		switch(arrowStyle)
		{
			case HList.STYLE_ARROW_TOP:
				arrowSubKey = 'arrow_top';
				break;
			case HList.STYLE_ARROW_DOWN:
				arrowSubKey = 'arrow_down';
				break;
		}
		
		DllLoaderManager.getInstance().getUIMaterial(UiNameConst.GENERAL_ICON,subKey,loadCompleteHandler);
		if(arrowSubKey)
			DllLoaderManager.getInstance().getUIMaterial(UiNameConst.GENERAL_ICON,arrowSubKey,arrowLoadCompleteHandler);
	}
	
	override protected function init():void
	{
		setSize(94,27);
		super.init();
	}
	
	private function loadCompleteHandler(c:*,key:String = ''):void
	{
		_mc = Helpers.drawMc(c,_mc);
		addChild(_mc);
		
		_width = _mc.width;
		_height = _mc.height;
		
		_nameTf = Helpers.FormatTextField(_hb,0,0,_width,TextFieldAutoSize.LEFT,0x317BAF,FontManager.getInstance().embeddedFontBName,16,TextFormatAlign.LEFT);
		_nameTf.mouseEnabled = false;
		_nameTf.text = _text;
		
		refreshHb();
		addEventListener(MouseEvent.ROLL_OVER,hoverHandler);
		addEventListener(MouseEvent.CLICK,clickHandler);
	}
	
	private function arrowLoadCompleteHandler(c:*,key:String = ''):void
	{
		_arrowMc = Helpers.drawMc(c,_arrowMc);
		_hb.addChild(_arrowMc);
		_arrowMc.y = (_height - _arrowMc.height)*.5;
		
		refreshHb();
	}
	
	private function refreshHb():void
	{
		this.addChild(_hb);
		_hb.draw();
		_hb.x = (_width - _hb.width)*.5;
	}
	
	private function clickHandler(e:MouseEvent):void
	{
		_clickCallHandler(_index);
	}
	
	private function hoverHandler(e:MouseEvent):void
	{
		_over = true;
		addEventListener(MouseEvent.ROLL_OUT,outHandler);
		selected = _selected;
	}
	
	private function outHandler(e:MouseEvent):void
	{
		_over = false;
		removeEventListener(MouseEvent.ROLL_OUT,outHandler);
		selected = _selected;
	}

	public function get selected():Boolean
	{
		return _selected;
	}

	public function set selected(value:Boolean):void
	{
		_selected = value;
		if(selected)
		{
			_mc.gotoAndStop(3);
			_arrowMc.gotoAndStop(3);
			_nameTf.textColor = 0xFFC915;
			_nameTf.filters = [FiltersEnum.TEXT_FILTERS3];
		}
		else
		{
			_nameTf.filters = [FiltersEnum.TEXT_FILTERS,FiltersEnum.TEXT_FILTERS2];
			_nameTf.textColor = 0x317BAF;
			if(_over)
			{
				_mc.gotoAndStop(2);
				_arrowMc.gotoAndStop(2);
			}
			else
			{
				_mc.gotoAndStop(1);
				_arrowMc.gotoAndStop(1);
			}
		}
	}
	private var _selected:Boolean;
	private var _over:Boolean;
}